﻿Imports System.Text
Imports System.Net
Imports System.IO
Public Class FTPHelper
    Private _ftpAddress As String
    Private _ftpUsername As String
    Private _ftpPW As String
    Private _currentDirectory As String
    Private _URLWithDirectory As String


    Public Sub New(ByVal URL As String, ByVal username As String, ByVal pw As String)
        If Left(URL, 6) = "ftp://" Then
            _ftpAddress = URL
        Else
            _ftpAddress = "ftp://" & URL
        End If
        If Not Right(_ftpAddress, 1) = "/" Then
            _ftpAddress = _ftpAddress & "/"
        End If

        _ftpUsername = username
        _ftpPW = pw
    End Sub

    Public Property FTPAddress() As String
        Get
            Return _ftpAddress
        End Get
        Set(ByVal value As String)
            _ftpAddress = value
        End Set
    End Property

    Public Property FTPUsername() As String
        Get
            Return _ftpUsername
        End Get
        Set(ByVal value As String)
            _ftpUsername = value
        End Set
    End Property

    Public Property FTPPW() As String
        Get
            Return _ftpPW
        End Get
        Set(ByVal value As String)
            _ftpPW = value
        End Set
    End Property
    Public Property URLWithDirectory()
        Get
            Return _URLWithDirectory
        End Get
        Set(ByVal value)
            _URLWithDirectory = value
        End Set
    End Property

    Public Function ListDirectory(ByVal path As String) As String()
        Dim myFTP As FtpWebRequest
        Dim TempFileLists As New ArrayList
        If Not Right(FTPAddress, 1) = "/" Then
            FTPAddress = FTPAddress & "/" & path
        Else
            FTPAddress = FTPAddress & path
        End If
        URLWithDirectory = FTPAddress

        myFTP = FtpWebRequest.Create(New Uri(FTPAddress))
        myFTP.UseBinary = True
        myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)




        myFTP.Method = WebRequestMethods.Ftp.ListDirectory
        Try


            Dim DirectoryResponse As WebResponse
            DirectoryResponse = myFTP.GetResponse

            Dim reader As New StreamReader(DirectoryResponse.GetResponseStream, System.Text.Encoding.Default)
            Dim line As String = reader.ReadLine

            While Not line Is Nothing
                TempFileLists.Add(line)

                line = reader.ReadLine

            End While
            reader.Close()
            DirectoryResponse.Close()



        Finally
            If TempFileLists.Count = 0 Then
                TempFileLists = Nothing
            End If
        End Try


        If Not TempFileLists Is Nothing Then
            Dim FileLists(TempFileLists.Count - 1) As String
            TempFileLists.CopyTo(FileLists)
            Return FileLists
        Else
            Return Nothing
        End If


    End Function

    Public Function MoveFolderLayerUp() As String()
        Dim result As String()
        Dim i As Int32 = 0
        i = _ftpAddress.LastIndexOf("/")
        If i > 5 Then
            FTPAddress = _ftpAddress.Substring(0, i)
            URLWithDirectory = FTPAddress

        End If
        result = (LoadCurrentDirectory())


        Return result
    End Function
    Private Sub testFTP()

        Dim myFTP As FtpWebRequest
        Dim result As New StringBuilder
        myFTP = FtpWebRequest.Create(New Uri(FTPAddress))
        '        ftp.Credentials.GetCredential


        myFTP.UseBinary = True
        myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)
        myFTP.Method = WebRequestMethods.Ftp.ListDirectory
        Dim DirectoryResponse As WebResponse
        DirectoryResponse = myFTP.GetResponse

        Dim reader As New StreamReader(DirectoryResponse.GetResponseStream)
        Dim line As String = reader.ReadLine

        While Not line Is Nothing
            If Not line Is Nothing Then
                result.Append(line)
                result.Append("\n")
            End If

            line = reader.ReadLine

        End While
        result.Remove(result.ToString().LastIndexOf("\n"), 1)
        reader.Close()
        DirectoryResponse.Close()

        MsgBox(result.ToString)
    End Sub

    Public Function DownloadFile(ByVal filename As String, ByVal destination As String) As Boolean
        Dim result As Boolean = False

        Dim myFTP As FtpWebRequest

        If Right(destination, Len(filename)) <> filename Then
            destination = destination + "\" + filename
        End If
        Dim fileToDownload As String = ""
        If Right(URLWithDirectory, 1) <> "/" Then
            URLWithDirectory = URLWithDirectory & "/"
        End If
        Dim fOutputStream As New FileStream(destination, FileMode.Create)
        myFTP = FtpWebRequest.Create(New Uri(URLWithDirectory & filename))
        myFTP.Method = WebRequestMethods.Ftp.DownloadFile
        myFTP.UseBinary = True
        myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)

        Dim response As FtpWebResponse
        response = myFTP.GetResponse
        Dim contentLen As Long = 0
        Dim ftpStream As Stream
        ftpStream = response.GetResponseStream
        contentLen = response.ContentLength

        Dim bufferSize As Int32 = 2048
        Dim readCount As Int32 = 0

        Dim buffer(bufferSize) As Byte

        readCount = ftpStream.Read(buffer, 0, bufferSize)

        While readCount > 0
            fOutputStream.Write(buffer, 0, readCount)
            readCount = ftpStream.Read(buffer, 0, bufferSize)
        End While
        ftpStream.Close()
        fOutputStream.Close()
        response.Close()


        result = True



        Return result
    End Function

    Public Function UploadFile(ByVal fileToUpdate As String, ByVal FTP_FileName As String) As Boolean
        Dim result As Boolean = False

        If File.Exists(fileToUpdate) Then


            Dim fInfo As New FileInfo(fileToUpdate)

            Dim myFTP As FtpWebRequest
            If Not Right(FTPAddress, 1) = "/" Then
                FTPAddress = FTPAddress & "/"
            End If


            myFTP = FtpWebRequest.Create(New Uri(FTPAddress + FTP_FileName))
            myFTP.UseBinary = True
            myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)
            myFTP.KeepAlive = False
            myFTP.Method = WebRequestMethods.Ftp.UploadFile

            myFTP.ContentLength = fInfo.Length

            Dim bufferLength As Int32 = 2048
            Dim contentLen As Int32 = 0
            Dim buffer(bufferLength) As Byte
            Dim fStream As FileStream
            fStream = fInfo.OpenRead


            Dim responseStream As Stream
            responseStream = myFTP.GetRequestStream
            contentLen = fStream.Read(buffer, 0, bufferLength)

            While (contentLen <> 0)
                responseStream.Write(buffer, 0, contentLen)
                contentLen = fStream.Read(buffer, 0, bufferLength)

            End While
            responseStream.Close()
            fStream.Close()
            result = True

        End If


        Return result
    End Function

    Public Sub TestConnection()
        Dim myFTP As FtpWebRequest
        Dim result As New StringBuilder
        myFTP = FtpWebRequest.Create(New Uri(FTPAddress))
        myFTP.Method = WebRequestMethods.Ftp.ListDirectory
        myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)



        Dim DirectoryResponse As WebResponse
        DirectoryResponse = myFTP.GetResponse
        MsgBox("true")

        DirectoryResponse.Close()


    End Sub
    Public Function LoadCurrentDirectory() As String()
        Dim myFTP As FtpWebRequest
        Dim TempFileLists As New ArrayList


        myFTP = FtpWebRequest.Create(New Uri(URLWithDirectory))
        myFTP.UseBinary = True
        myFTP.Credentials = New NetworkCredential(FTPUsername, FTPPW)
        myFTP.Method = WebRequestMethods.Ftp.ListDirectory

        Dim DirectoryResponse As WebResponse
        DirectoryResponse = myFTP.GetResponse

        Dim reader As New StreamReader(DirectoryResponse.GetResponseStream, System.Text.Encoding.Default)
        Dim line As String = reader.ReadLine

        While Not line Is Nothing
            If Not line Is Nothing Then
                TempFileLists.Add(line)
            End If
            line = reader.ReadLine


        End While
        reader.Close()
        DirectoryResponse.Close()

        Dim FileLists(TempFileLists.Count - 1) As String
        TempFileLists.CopyTo(FileLists)
        Return FileLists
    End Function

End Class
